package com.jeecms.cms.manager.main.impl;

import com.jeecms.cms.dao.main.BranchDao;
import com.jeecms.cms.entity.assist.Branch;
import com.jeecms.cms.manager.main.BranchMng;
import com.jeecms.common.hibernate4.Updater;
import com.jeecms.common.page.Pagination;
import com.jeecms.core.entity.CmsSite;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class BranchMngImpl implements BranchMng {

    @Override
    public List<Branch> getTopList(Integer siteId, boolean hasEnterpriseOnly) {
        return branchDao.getTopList(siteId, hasEnterpriseOnly, true);
    }

    @Override
    public List<Branch> getTopListByRigth(Integer userId, Integer siteId, boolean hasEnterpriseOnly) {
        return branchDao.getTopListByRigth(userId, siteId, true, true);
    }

    @Override
    public List<Branch> getTopListForTag(Integer siteId, boolean hasEnterpriseOnly) {
        return null;
    }

    @Override
    public Pagination getTopPageForTag(Integer siteId, boolean hasEnterpriseOnly, int pageNo, int pageSize) {
        return branchDao.getTopPageForTag(siteId, hasEnterpriseOnly, pageNo, pageSize);
    }

    @Override
    public List<Branch> getChildList(Integer parentId, boolean hasEnterpriseOnly) {
        return branchDao.getChildList(parentId, hasEnterpriseOnly, true);
    }

    @Override
    public List<Branch> getChildListByRight(Integer userId, Integer siteId, Integer parentId, boolean hasEnterpriseOnly) {
        return branchDao.getChildListByRight(userId, siteId, parentId, true, true);
    }

    @Override
    public List<Branch> getChildListForTag(Integer parentId, boolean hasEnterpriseOnly) {
        return null;
    }

    @Override
    public List<Branch> getBottomList(Integer siteId, boolean hasEnterpriseOnly) {
        return null;
    }

    @Override
    public Pagination getChildPageForTag(Integer parentId, boolean hasEnterpriseOnly, int pageNo, int pageSize) {
        return branchDao.getChildPageForTag(parentId, hasEnterpriseOnly, pageNo, pageSize);
    }

    @Override
    public Branch findById(Integer id) {
        return branchDao.findById(id);
    }

    @Override
    public Branch save(Branch bean, CmsSite site, Integer parentId, boolean isCopy) {
        bean.setSite(site);
        if(parentId != null){
            bean.setParent(new Branch(parentId));
        }
        return branchDao.save(bean);
    }

    @Override
    public Branch copy(Integer bid, Integer siteId) {
        return null;
    }

    @Override
    public Branch update(Branch bean,Integer siteId, Integer parentId, boolean isCopy) {
        // 更新主表
        if(parentId != null){
            bean.setParent(findById(parentId));
        }else {
            bean.setParent(null);
        }
        Updater<Branch> updater = new Updater<Branch>(bean);
        bean = branchDao.updateByUpdater(updater);
        return bean;
    }

    @Override
    public Branch deleteById(Integer id) {
        Branch entity = branchDao.findById(id);
        /*for (CmsGroup group : entity.getViewGroups()) {
            group.getViewChannels().remove(entity);
        }
        for (CmsGroup group : entity.getContriGroups()) {
            group.getContriChannels().remove(entity);
        }*/
        entity = branchDao.deleteById(id);
        return entity;
    }

    @Override
    public Branch[] deleteByIds(Integer[] ids) {
        Branch[] beans = new Branch[ids.length];
        for (int i = 0, len = ids.length; i < len; i++) {
            beans[i] = deleteById(ids[i]);
        }
        return beans;
    }

    @Override
    public Branch[] updatePriority(Integer[] ids, Integer[] priority) {
        int len = ids.length;
        Branch[] beans = new Branch[len];
        for (int i = 0; i < len; i++) {
            beans[i] = findById(ids[i]);
            beans[i].setPriority(priority[i]);
        }
        return beans;
    }

    @Override
    public void initWorkFlow(Integer workflowId) {

    }

    @Override
    public String checkDelete(Integer id) {
        String msg = null;
        String cnt = branchDao.checkDelete(id);
        Integer result = Integer.parseInt(cnt);
        if(result > 0){
            msg = "branch.error.cannotDeleteBranch";
        }
        return msg;
    }

    private BranchDao branchDao;

    @Autowired
    public void setBranchDao(BranchDao branchDao) {
        this.branchDao = branchDao;
    }
}
